package com.ddd.mill.infrastructure.utils;

import com.ddd.mill.domain.aggregate.constant.user.constant.ApplicationInfo;
import com.ddd.mill.domain.aggregate.constant.user.constant.UserAppFromEnum;
import com.ddd.mill.infrastructure.constant.HeaderFields;

import org.springframework.http.HttpHeaders;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * Servlet工具类
 */
public abstract class ServletUtils {
    /**
     * 获取String参数
     */
    public static String getParameter(String name) {

        return getRequest().getParameter(name);
    }

    /**
     * 获取request
     */
    public static HttpServletRequest getRequest() {
        return getRequestAttributes().getRequest();
    }

    /**
     * 获取response
     */
    public static HttpServletResponse getResponse() {
        return getRequestAttributes().getResponse();
    }

    /**
     * 获取session
     */
    public static HttpSession getSession() {
        return getRequest().getSession();
    }

    public static ServletRequestAttributes getRequestAttributes() {
        RequestAttributes attributes = RequestContextHolder.getRequestAttributes();
        return (ServletRequestAttributes) attributes;
    }

    public static Object getRequestAttributeByName(String name) {
        ServletRequestAttributes requestAttributes = getRequestAttributes();
        return requestAttributes.getAttribute(name, RequestAttributes.SCOPE_REQUEST);
    }

    /**
     * 将字符串渲染到客户端
     *
     * @param response 渲染对象
     * @param string   待渲染的字符串
     * @return null
     */
    public static String renderString(HttpServletResponse response, String string) {
        try {
            response.setStatus(200);
            response.setContentType("application/json");
            response.setCharacterEncoding("utf-8");
            response.getWriter().print(string);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String getRemoteHost() {
        HttpServletRequest request = getRequest();
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        if (ip.equals("0:0:0:0:0:0:0:1")) {
            ip = "127.0.0.1";
        }
        // "***.***.***.***".length() = 15 逗号 分隔符
        if (ip != null && ip.length() > 15) {
            int separatorIdx = ip.indexOf(",");
            if (separatorIdx > 0) {
                ip = ip.substring(0, separatorIdx);
            } else {
                ip = "127.0.0.1";
            }
        }
        return ip;
    }

    /**
     * 获取app-key
     *
     * @return
     */
    public static String getAppId() {
        return getRequest().getHeader(HeaderFields.APP_ID);
    }

    /**
     * 获取token
     *
     * @return
     */
    public static String getToken() {
        String token = getRequest().getHeader(HttpHeaders.AUTHORIZATION);
        if (StringUtils.isNotBlank(token)) {
            return token.replace("QNT ", "");
        }
        return null;
    }

    /**
     * 获取token
     *
     * @return
     */
    public static UserAppFromEnum getAppfrom() {
        String appId=getAppId();
        if (StringUtils.isNotBlank(appId)){
            if (ApplicationInfo.YYT_APP.equals(appId)){
                return UserAppFromEnum.YYT_APP;
            }
            if (ApplicationInfo.YYT_MINI_PROGRAM.equals(appId)){
                return UserAppFromEnum.YYT_MINI_PROGRAM;
            }
            if (ApplicationInfo.YYT_CLIENT_WEB.equals(appId)){
                return UserAppFromEnum.YYT_CLIENT_WEB;
            }
            if (ApplicationInfo.YYT_OPERATION_WEB.equals(appId)){
                return UserAppFromEnum.YYT_OPERATION_WEB;
            }
            if (ApplicationInfo.ZTB_APP.equals(appId)){
                return UserAppFromEnum.ZTB_APP;
            }
            if (ApplicationInfo.ZTB_MINI_PROGRAM.equals(appId)){
                return UserAppFromEnum.ZTB_MINI_PROGRAM;
            }
            if (ApplicationInfo.ZTB_OPERATION_WEB.equals(appId)){
                return UserAppFromEnum.ZTB_OPERATION_WEB;
            }
        }
        return null;
    }
}
